/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-20
 * Time: 13:31
 */
//https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/submissions/654790834/
public class Test {
    public static int[] searchRange(int[] nums, int target) {
        int size=nums.length;
        if(size==0){
            return new int[]{-1,-1};
        }
        int p1=0,p2=size-1,p=(p1+p2)/2,p0=(p1+p2)/2;
        while(p1<=p2){
            if(nums[p]==target &&( (p+1<size && nums[p]!=nums[p+1]) || p+1==size ) ){
                break;
            }
            if(nums[p]<=target){
                p1=p+1;
            }
            else if(nums[p]>target){
                p2=p-1;
            }
            p=(p1+p2)/2;
        }
        if(nums[p]!=target){
            return new int[]{-1,-1};
        }
        p1=0;p2=p;
        while(p1<=p2){
            if(nums[p0]==target &&( (p0-1>=0 && nums[p0]!=nums[p0-1]) || p0-1<0)){
                break;
            }
            if(nums[p0]<target){
                p1=p0+1;
            }
            else if(nums[p0]>=target){
                p2=p0-1;
            }
            p0=(p1+p2)/2;
        }
        return new int[]{p0,p};
    }
    public static void main(String[] args) {
        int[] arr={5,7,7,8,8,10};
        int[] brr=searchRange(arr,8);
        System.out.println(brr[0]+" "+brr[1]);

    }
}
